package com.example.orderproject;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.orderproject.Bean.MenuForm;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class MenuInFoActivity extends AppCompatActivity {
    private ScrollView activityMenuFromInFo;
    private LinearLayout ll;
    private Button backButton;
    private LinearLayout caiming;
    private LinearLayout danjia;
    private LinearLayout shuliang;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_in_fo);
        Bmob.initialize(this,"dc2904f179b87449f83932778d2deb0d");
        activityMenuFromInFo = (ScrollView) findViewById(R.id.activity_menu_from_in_fo);
        ll = (LinearLayout) findViewById(R.id.ll);
        backButton = (Button) findViewById(R.id.back_button);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BmobQuery<MenuForm> query = new BmobQuery<MenuForm>();

                query.findObjects(new FindListener<MenuForm>() {
                    @Override
                    public void done(List<MenuForm> object, BmobException e) {
                        if(e==null){

                            for (MenuForm gameScore : object) {


                                MenuForm menu = new MenuForm();
                                menu.setObjectId(gameScore.getObjectId());
                                menu.delete(new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                        if(e==null){
//                                            Toast.makeText(MenuInFoActivity.this, "完成订单", Toast.LENGTH_SHORT).show();
                                            finish();
                                        }else{
                                            Toast.makeText(MenuInFoActivity.this, "订单失败", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });

                            }
                        }else{
                            Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                        }
                    }
                });


            }
        });
        caiming = (LinearLayout) findViewById(R.id.caiming);
        danjia = (LinearLayout) findViewById(R.id.danjia);
        shuliang = (LinearLayout) findViewById(R.id.shuliang);
        QueryMenuFrom();
    }
    public void QueryMenuFrom(){
        BmobQuery<MenuForm> query = new BmobQuery<MenuForm>();

//执行查询方法
        query.findObjects(new FindListener<MenuForm>() {
            @Override
            public void done(List<MenuForm> object, BmobException e) {
                if(e==null){
                    for (MenuForm menuFrom :object){
                        TextView textView1=new TextView(MenuInFoActivity.this);
                        textView1.setText(menuFrom.getMenu_name());
                        caiming.addView(textView1);
                        TextView textView2=new TextView(MenuInFoActivity.this);
                        textView2.setText(menuFrom.getPrice());
                        danjia.addView(textView2);
                        TextView textView3=new TextView(MenuInFoActivity.this);
                        textView3.setText(menuFrom.getNumber());
                        shuliang.addView(textView3);


                    }
                    Log.i("bmob","成功："+e.getMessage()+","+e.getErrorCode());

                }else{
                    Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });


    }
}
